﻿import Vue from 'vue';
import Vuex from 'vuex';
// 引入 axios
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
    state: {
        // username:'',//存储用户信息
        title: '',
        poster: '',
        rating: null,
        introduction: '',
        _id: '',
        //需要刷新的组件名字
        refreshName: '',
        refreshParams: '',
        //关于图片上传
        imgURL: '',
    },
    getters: {


    },
    mutations: {
        //改变需要刷新的组件
        changeRefreshName(state, payload) {
            console.log('执行到了mutations')
            state.refreshName = payload.refreshName
            //如果存在关键字
            if (typeof payload.params != 'undefined') {
                state.refreshParams = payload.params
            }
        },
        //前端添加信息并修改state的状态(联动1)
        changeTheState(state, value) {
            state.title = value.title;
            state.poster = value.poster;
            state.rating = value.rating;
            state.introduction = value.introduction;
        },
        //改变用户id
        changeuserID(state, value) {
            state._id = value;
        },
        // 传入DETAIL的ID
        changeID(state, value) {
            state._id = value
        },
        // 改变图片地址
        changeImgURL(state, value_1) {
            state.imgURL = value_1.url;
            console.log(state.imgURL);
            console.log("成功修改imgURL的值");
        }
    },
    actions: {
        // 前后端数据链接axios
        //获取全部信息
        getMessage(context, payload) {
            axios.get('/api/message')
                .then(res => {
                    payload.callback(res);
                })
                .catch(err => {
                    console.log(err)
                })
        },
        //获取全部用户信息
        getUser(context, payload) {
            axios.get('api/users')
                .then(res => {
                    payload.callback(res);

                })
                .catch(err => {
                    console.log(err)
                })
        },
        // 获得城市名
        getcity(context,payload){
            axios.get('/api/city')
            .then(res =>{
                payload.callback(res);
            })
            .catch(err =>{
                console.log(err)
            })
        },
        // 访问后端添加电影信息的方法
        addMessage(context, payload) {
            axios.post('/api/message',
                {
                    title: context.state.title,
                    poster: context.state.poster,
                    introduction: context.state.introduction,
                    rating: context.state.rating,
                })
                .then(res => {
                    console.log(res.data);
                    // console.log(payload);

                })
                .catch(e => {
                    console.log(e)
                })
        },
        //前端添加信息，通过action调用mutations的方法(联动1)
        adds({ commit }, value, context) {
            commit("changeTheState", value);
        },
        //删除信息的方法+删除该信息的所有评论+删除该信息的所有加入情况(全部都是根据detail的ID进行删除) 载荷
        deleteMessage(context, payload) {
            // console.log(context.state._id);
            let id = context.state._id
            axios.delete(`/api/message/${id}`)
                .then(res => {
                    console.log(res.data);
                    console.log("信息删除成功！");
                })
                .catch(e => console.log(e));
            axios.delete(`/api/Allcritiques/${id}`)
                .then(res => {
                    console.log(res.data);
                    console.log("该文章的所有评论删除成功！");
                });
            axios.delete(`/api/join/${id}`)
                .then(res => {
                    console.log(res.data);
                    console.log("该文章所有加入信息删除成功！");
                });

        },
        // 删除用户
        deleteUser(context, payload) {
            let id = context.state._id
            axios.delete(`/api/users/${id}`)
                .then(res => {
                    console.log(res.data);
                    console.log("该用户删除成功");
                })
        },
        //改变当前用户id
        deleteusers({ commit }, value, context) {
            commit("changeuserID", value)
        },
        //改变当前ID
        deletes({ commit }, value, context) {
            commit("changeID", value);
        },
        //往服务器传输文件并修改返回的路径
        doUploadImg(context, value) {
            axios.post('/api/upload', value)
                .then(res => {
                    console.log(res.data);
                    let value_1 = res.data;
                    context.commit("changeImgURL", value_1);
                    console.log("上传成功！");
                })
                .catch(e => {
                    console.log(e)
                })
        },
    }
})
